#include "../include/web_page.hpp"

#include <regex>
#include <fstream>

using std::regex;
using std::smatch;
using std::regex_search;
using std::ifstream;
using std::cout;
using std::endl;

namespace gcj
{

WebPage::WebPage(const string &doc)
: _doc(doc)
{  
    _jieba = SplitTool::getInstance();

    //获取文档的id
    size_t beg = _doc.find("<docid>");
    size_t end = _doc.find("</docid>");
    if(beg == string::npos || end == string::npos) {
        _docID = -1;
    }
    else {
        string tmp = _doc.substr(beg + 7, end - beg - 1);
        _docID = atoi(tmp.c_str());
    }

    //====文档的Title====
    beg = _doc.find("<title>");
    end = _doc.find("</title>");
    if(beg == string::npos || end == string::npos) {
        _docTitle = "";
    }
    else {
        _docTitle = _doc.substr(beg + 7, end - beg - 7);
    }

    //====文档的URL====
    beg = _doc.find("<link>");
    end = _doc.find("</link>");
    if(beg == string::npos || end == string::npos) {
        _docUrl = "";
    }
    else {
        _docUrl = _doc.substr(beg + 6, end - beg - 6);
    }
    
    //获取文档的主要内容
    /* std::cout << "_doc.size() = " << _doc.size() << std::endl; */
    beg = _doc.find("<content>");
    end = _doc.find("</content>");
    if(beg == string::npos || end == string::npos) {
        _docContent = "";
    }
    else {
        _docContent = _doc.substr(beg+13, end-13-beg);
    }
}

long WebPage::getDocID() {
    return _docID;
}

string WebPage::getTitle() {
    return _docTitle;
}

string WebPage::getUrl() {
    return _docUrl;
}

string WebPage::getContent() {
    return _docContent;
}

unordered_map<string, int> & WebPage::getWordsMap() {
    //获取文档的词频统计map
    /* getContent(); */
    if(_docContent.size() == 0) {
        return _wordsMap;
    }
    vector<string> wordsVec = _jieba->cut(_docContent);

    for(auto & word : wordsVec) {
        if(_wordsMap.find(word) == _wordsMap.end()) {
            _wordsMap[word] = 1;
        }
        else {
            ++_wordsMap[word];
        }
    }
    /* std::cout << "wordsVec.size(): " << wordsVec.size() << std::endl; */

    return _wordsMap;
}

vector<string> WebPage::calcTopK(vector<string>&wordsVec, int k) {
    //TODO

    return _topWords;
}

}//end of namespace gcj

